class HomepageController < ApplicationController
  require 'hpricot'
  require 'open-uri'
  
  def index
    @wards = Ward.find(:all)
    @councillors = Councillor.find(:all)
    @parties = Party.find(:all)
	@meetings = Meeting.find(:all, :limit => 14, :order => 'date desc', :conditions => ["date < ?", Date.today.to_s(:iso)])
	
	respond_to do |format|
      format.html # index.html.erb
	  format.rss
    end
  end
  
  
  def postcode
    
  end
  
  # ref : https://gist.github.com/7117ee510172f7921e05
  def postcode_lookup  
    @postcode = params[:postcode][:postcode]
    logger.debug "Postcode="+@postcode 
    #"http://www.writetothem.com/"
    postcode_lookup_url = "http://www.writetothem.com/who?pc="+ @postcode.sub(" ","%20") #m20%204pb    
    #lookup
    doc = Hpricot(open(postcode_lookup_url))
    #logger.debug "DOC="+doc.inspect
    #ward
    @ward=doc.to_html.scan(/Your \d (.*) Councillors/).to_s
    
    @councillors = Ward.find_by_name(@ward).councillors
    #borough
    
  end
  
end